<!DOCTYPE html>
<html>

<head>

    {include file="Public:header"}
    <style>

    </style>
</head>

<body style="color:#333;padding:3px;">
<div>
    <form action="" class="form-inline">
        <input type="hidden" name="wllx" value="{$data.wllx}">
        <table class="row-margin-top tab-content table-bordered  table" border="1" style="margin-bottom: 0">
            <tr>
                <td style="min-width: 160px;width: 160px">名称<span class="colorRed">*</span>：</td>
                <td colspan="3">
                    <div class="wlNameText">
                        <div class="col-xs-4">
                            <div class="input-group" id="jxs_input" style="display: none;">
                                <input type="text"   placeholder="请输入关键字或空格查询" class="form-control inputremindPeple" id="wlName"  value="{:isset($supply)?$supply.jxs:''}">
                                <div class="input-group-btn">
                                    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                                        <span class="caret"></span>
                                    </button>
                                    <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                    </ul>
                                </div>
                            </div>
                            <div id="jxs_queding" class="selectedOnlyOne" >
                                <input type="hidden" name="title" value="{$data.title}">
                                <input type="hidden" name="pro_no" value="{$data.pro_no}">
                                <input type="hidden" name="title_name" value="{$data.title_name}">
                                <span class="inin">{$data.title_name}</span>
                                <span class="close">×</span>
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <a href="javascript:;" id="selectName">选择</a>
                            <span class="lr_5"></span>
                            <span class="helpText">帮</span>
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <td style="min-width: 160px;width: 160px">供应商<span class="colorRed">*</span>：</td>
                <td colspan="3">
                    <div class="wlNameText">
                        <div class="col-xs-4">
                            <div class="input-group" id="gys_input" style="display: none;" >
                                <input type="text"   placeholder="请输入关键字或空格查询" class="form-control inputremindPeple" id="gysName"  value="{:isset($supply)?$supply.jxs:''}">
                                <div class="input-group-btn">
                                    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                                        <span class="caret"></span>
                                    </button>
                                    <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                    </ul>
                                </div>
                            </div>
                            <div id="gys_queding" class="selectedOnlyOne" style="display: block;">
                                <input type="hidden" name="gys" value="{$data.gys}">
                                <input type="hidden" name="gys_name" value="{$data.gys_name}">
                                <span class="inin">{$data.gys_name}</span>
                                <span class="close">×</span>
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <a href="javascript:;" id="selectGys">选择</a>
                            <span class="lr_5"></span>
                            <span class="helpText">帮</span>
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <td style="min-width: 160px;width: 160px">单位<input type="hidden" name="unit" value="{$data.unit}"></td>
                <td id="unitText">{$data.unit}</td>
                <td style="min-width: 160px;width: 160px">单价(元)<span class="colorRed">*</span></td>
                <td><input type="text" class="form-control" name="dj" id="univalent" value="{$data.dj}"></td>
            </tr>
            <tr>
                <td style="min-width: 160px;width: 160px">数量<span class="colorRed">*</span></td>
                <td><input type="text" class="form-control" name="num" id="num" value="{$data.num}"></td>
                <td style="min-width: 160px;width: 160px">总金额(元)<input type="hidden" name="zje" value="{$data.zje}"></td>
                <td id="aggregateAmount">{$data.zje}</td>
            </tr>
            <tr>
                <td style="min-width: 160px;width: 160px">备注：</td>
                <td colspan="3">
                    <textarea name="bz" id="" class="form-control"
                              style="width: 60%;user-select:none;height: 50px;">{$data.bz}</textarea><span
                        style="vertical-align: bottom;">(50)</span>
                </td>
            </tr>
        </table>
        <div class="text-center padding-10">
            <button class="btn btn-primary" type="button" id="savebtn">保存</button>
            <button class="btn btn-warning" style="margin-left: 20px" onclick="back()">返回</button>
        </div>
    </form>
</div>
<!-- 全局js -->
<script src="__STATIC__/layout/js/jquery.min.js?v=2.1.4"></script>
<script src="__STATIC__/layout/js/bootstrap.min.js?v=3.3.6"></script>
<!-- <script src="js/plugins/metisMenu/jquery.metisMenu.js"></script> -->
<script src="__STATIC__/layout/js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<!-- <script src="js/plugins/layer/laydate/laydate.js"></script> -->
<script src="__STATIC__/layout/js/plugins/sweetalert/sweetalert.min.js"></script>
<script src="__STATIC__/layout/js/plugins/layer/layer.min.js"></script>
<script src="__STATIC__/layout/js/plugins/suggest/bootstrap-suggest.min.js"></script>
<script>
    var url = ''
    //搜索销售物料
    {if $data.wllx==1}
    url = "{:url('bs/xswl2')}"
    //生产物料
    {elseif $data.wllx==2}
    url = "{:url('bs/scwl')}"
    //成品
    {else}
    url = "{:url('bs/products')}"
    {/if}
    $("#wlName").bsSuggest({
        url: url,
        effectiveFields: ["id", "title"],
        /*searchFields: [ "shortAccount"],*/
        effectiveFieldsAlias: {title: "名称"},
        ignorecase: true,
        showHeader: true,
        showBtn: false,     //不显示下拉按钮
        delayUntilKeyup: true, //获取数据的方式为 firstByUrl 时，延迟到有输入/获取到焦点时才请求数据
        idField: "id",
        keyField: "title",
        clearable: true
    }).on('onDataRequestSuccess', function (e, result) {
        $(this).data('rs',JSON.stringify(result.value))
    }).on('onSetSelectValue', function (e, keyword) {
        $(this).val('')
        var rs = $(this).data('rs')
        var unit,lingshou_price,pro_no
        rs = JSON.parse(rs)
        for(var i=0;i<rs.length;i++){
            if(rs[i].id==keyword.id){
                unit = rs[i].unit
                lingshou_price = rs[i].lingshou_price
                pro_no = rs[i].pro_no
            }

        }
        $('#jxs_queding').find('.inin').html(keyword.key)
        $('#jxs_queding').find('[name=title_name]').val(keyword.key)
        $('#jxs_queding').find('[name=title]').val(keyword.id)
        $('#jxs_queding').find('[name=pro_no]').val(pro_no)
        $('#jxs_queding').show()
        $('#jxs_input').hide()
        $('#unitText').html(unit)
        $('[name=unit]').val(unit)
        $('[name=dj]').val(lingshou_price)
        var zje = parseFloat(lingshou_price)*parseInt($('#num').val())
        $('#aggregateAmount').html(zje)
        $('[name=zje]').val(zje)
    });

    $('body').on('click','.selectedOnlyOne .close',function () {
        $(this).closest('.selectedOnlyOne').hide()
        $(this).closest('.selectedOnlyOne').prev('div').show()
    })

    $('#num').on('input',function () {
        var zje = parseFloat($('[name=dj]').val())*parseInt($('#num').val())
        $('#aggregateAmount').html(zje)
        $('[name=zje]').val(zje)
    })

        {if $data.wllx==1}
        $("#selectName").click(function () {
            layer.open({
                type: 2,
                skin: 'layui-layer-rim', //加上边框
                area: ['720px', '370px'], //宽高
                title: "销售物料列表",
                content: 'xiaoshou_list.html'
            });
        })
        {elseif $data.wllx==2}
        $("#selectName").click(function () {
            layer.open({
                type: 2,
                skin: 'layui-layer-rim', //加上边框
                area: ['720px', '370px'], //宽高
                title: "生产物料列表",
                content: 'shengchan_list.html'
            });
        })
        {else}
        $("#selectName").click(function () {
            layer.open({
                type: 2,
                skin: 'layui-layer-rim', //加上边框
                area: ['720px', '370px'], //宽高
                title: "成品列表",
                content: '{:url("open/product3")}',
                btn:['确定','关闭'],
                yes:function (i,o) {
                    var a = $(o).find('iframe').contents()

                    var id =  a.find("#renyuan").val();
                    var title =  a.find("#renyuanname").val();

                    $.post('product',{id:id},function (data) {
                        var dw =  data.unit;
                        var dj =  data.lingshou_price;
                        var pro_no =  data.pro_no;
                        $('#unitText').html(dw)
                        $('[name=unit]').val(dw)
                        $('[name=dj]').val(dj)
                        var sl = $('[name=num]').val()
                        var zje = parseFloat(dj)*parseFloat(sl)
                        $('#aggregateAmount').html(zje)
                        $('[name=zje]').val(zje)

                        $('#jxs_queding').find('.inin').html(title)
                        $('#jxs_queding').find('[name=title]').val(id)
                        $('#jxs_queding').find('[name=title_name]').val(title)
                        $('#jxs_queding').find('[name=pro_no]').val(pro_no)
                        $('#jxs_queding').show()
                        $('#jxs_input').hide()

                        layer.close(i)
                    },'json')

                }
            });
        })
        {/if}

     var gys_link,gys_link2
    {if $data.wllx==1}
    gys_link = '{:url("bs/gys",["type_id"=>3])}'
    gys_link2 = '{:url("gys_list",["type_id"=>3])}'
    {elseif $data.wllx==2}
    gys_link = '{:url("bs/gys",["type_id"=>2])}'
    gys_link2 = '{:url("gys_list",["type_id"=>2])}'
    {else}
    gys_link = '{:url("bs/gys",["type_id"=>1])}'
    gys_link2 = '{:url("gys_list",["type_id"=>1])}'
    {/if}
    $("#gysName").bsSuggest({
        url: gys_link,
        /*effectiveFields: ["userName", "shortAccount"],
         searchFields: [ "shortAccount"],*/
        effectiveFieldsAlias: {title: "名称"},
        ignorecase: true,
        showHeader: true,
        showBtn: false,     //不显示下拉按钮
        delayUntilKeyup: true, //获取数据的方式为 firstByUrl 时，延迟到有输入/获取到焦点时才请求数据
        idField: "id",
        keyField: "title",
        clearable: true
    }).on('onDataRequestSuccess', function (e, result) {

    }).on('onSetSelectValue', function (e, keyword) {
        $(this).val('')
        $('#gys_queding').find('.inin').html(keyword.key)
        $('#gys_queding').find('[name=gys]').val(keyword.id)
        $('#gys_queding').find('[name=gys_name]').val(keyword.key)
        $('#gys_queding').show()
        $('#gys_input').hide()

    });


    $("#selectGys").click(function () {
        layer.open({
            type: 2,
            skin: 'layui-layer-rim', //加上边框
            area: ['720px', '370px'], //宽高
            title: "供应商列表",
            content: gys_link2
        });
    })

    $("#savebtn").click(function () {

        if ($("[name=title]").val() == "") {
            swal("", "名称不能为空", "error")
        } else if ($("[name=gys]").val() == "") {
            swal("", "供应商不能为空", "error")
        } else if ($("[name=dj]").val() == "") {
            swal("", "单价不能为空", "error")
        } else if ($("[name=num]").val() == "") {
            swal("", "数量不能为空", "error")
        } else {
            var title_name = $('#jxs_queding .inin').html()
            var pro_no = $('[name=pro_no]').val()
            var gys_name = $('#gys_queding .inin').html()
            var unit = $('[name=unit]').val()
            var dj = $('[name=dj]').val()
            var num = $('[name=num]').val()
            var zje = $('[name=zje]').val()

            var data = $(this).closest('form').serialize()
            data = escape(data)
            var index  = '{$data.index+1}'
            var str = '<tr data-data="'+data+'"><td>'+index+'</td>\n' +
                '<td><a href="javascript:;" class="btn btn-info modify_manual">修改</a><br/><a href="javascript:;" class="btn btn-info delThis">删除</a></td>' +
                '<td><a href="javascript:;">'+pro_no+'</a></td>' +
                '<td class="text-left">'+title_name+'</td>' +
                '<td class="text-left">'+gys_name+'</td>' +
                '<td>'+unit+'</td>' +
                '<td class="text-right">'+dj+'</td>' +
                '<td class="text-right">'+num+'</td>' +
                '<td class="text-right">'+zje+'</td></tr>'
            parent.$("#manualInfo table").css("display","block");
            parent.$("#manualInfo table").find('tbody tr:nth-child('+index+')').replaceWith(str);
            var $xj_num = 0
            var $xj_zje = 0

            parent.$("#manualInfo table").find('tbody').find('tr').each(function (i,o) {
                $xj_num += parseInt($(o).find('td:nth-child(8)').html())
                $xj_zje += parseInt($(o).find('td:last-child').html())
            })
            var xj = '<tr class="text-right font-bold"><td colspan="7">小计：</td><td>'+$xj_num+'</td><td>'+$xj_zje+'</td></tr>'
            parent.$("#manualInfo table").find('tfoot').html(xj);
            parent.layer.close(parent.layer.getFrameIndex(window.name))
            return false
            /*var param = $(this).closest('form').serialize()
            $.post('',param,function (data) {
                if(data.success){
                    swal({title:data.msg},function () {
//                        parent.layer.close(parent.layer.getFrameIndex(window.name))
                        parent.$("#manualInfo table").css("display", "block");
                        parent.$("#manualInfo table").find('tbody').html(data.str);
                        parent.layer.close(parent.layer.getFrameIndex(window.name))
                    })
                }else{
                    swal(data.msg,'','error')
                }
            },'json')
            return false*/

        }

    });

    function back() {
        parent.layer.close(parent.layer.getFrameIndex(window.name))
    }
</script>
</body>

</html>
